package com.valten;

import com.valten.job.TestJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

public class QuartzDemo {

    /**
     * SimpleTrigger和CronTrigger的区别：
     *         SimpleTrigger在具体的时间点执行一次或按指定时间间隔执行多次，
     *         CronTrigger按Cron表达式的方式去执行更常用。
     */
    public static void main(String[] args) throws SchedulerException {
        JobDetail jobDetail = JobBuilder.newJob(TestJob.class)
                .withIdentity("myJob", "myGroup")
                .usingJobData("name", "huangyuanli")
                .build();

        // SimpleTrigger
        // 每两秒执行一次, 直到永远
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("triggerName", "triggerGroup")
                .usingJobData("job_trigger_param","job_trigger_param1")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(2).repeatForever())
                .startNow()
                .build();

        // CronTrigger
        // 每两秒执行一次, 直到永远
        Trigger trigger2 = TriggerBuilder.newTrigger()
                .withIdentity("triggerName", "triggerGroup")
                .usingJobData("job_trigger_param","job_trigger_param1")
                .withSchedule(CronScheduleBuilder.cronSchedule("0/2 * * * * ?"))
                .startNow()
                .build();

        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = schedulerFactory.getScheduler();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }
}
